package com.grade.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.grade.common.core.text.Convert;
import com.grade.system.domain.GradeTemplate;
import com.grade.system.service.IGradeItemService;
import com.grade.system.service.IGradeTemplateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.grade.common.annotation.Log;
import com.grade.common.enums.BusinessType;
import com.grade.system.domain.GradeType;
import com.grade.system.service.IGradeTypeService;
import com.grade.common.core.controller.BaseController;
import com.grade.common.core.domain.AjaxResult;
import com.grade.common.utils.poi.ExcelUtil;
import com.grade.common.core.page.TableDataInfo;

/**
 * 评分标准分类Controller
 * 
 * @author grade
 * @date 2021-10-17
 */
@Controller
@RequestMapping("/system/type")
public class GradeTypeController extends BaseController
{
    private String prefix = "system/type";

    @Autowired
    private IGradeTypeService gradeTypeService;
    @Autowired
    private IGradeTemplateService gradeTemplateService;
    @Autowired
    private IGradeItemService gradeItemService;

    @RequiresPermissions("system:type:view")
    @GetMapping()
    public String type(Long id, ModelMap mmap)
    {
        GradeType gradeType = new GradeType();
        if(id!=null&& id>0){
            gradeType.setTemplateId(id);
            mmap.put("selectedTemplateId", id);
        }
        mmap.put("gradeType", gradeType);

        List<GradeTemplate> templateList = gradeTemplateService.selectGradeTemplateList(null);
        mmap.put("templateList", JSONObject.toJSON(templateList));
        List<GradeType> typeList = gradeTypeService.getGradeTypeOptionList(null);
        mmap.put("list", JSONObject.toJSON(typeList));
        return prefix + "/type";
    }

    /**
     * 查询评分标准分类列表
     */
    @RequiresPermissions("system:type:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GradeType gradeType)
    {
        startPage();
        List<GradeType> list = gradeTypeService.selectGradeTypeList(gradeType);
        return getDataTable(list);
    }

    /**
     * 导出评分标准分类列表
     */
    @RequiresPermissions("system:type:export")
    @Log(title = "评分标准分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GradeType gradeType)
    {
        List<GradeType> list = gradeTypeService.selectGradeTypeList(gradeType);
        ExcelUtil<GradeType> util = new ExcelUtil<GradeType>(GradeType.class);
        return util.exportExcel(list, "评分标准分类数据");
    }

    /**
     * 新增评分标准分类
     */
    @GetMapping("/add")
    public String add(Long templateId,ModelMap mmap)
    {
        mmap.put("templateId", templateId);
        List<GradeTemplate> templateList = gradeTemplateService.selectGradeTemplateList(null);
        mmap.put("templateList", JSONObject.toJSON(templateList));
        GradeType gradeType = new GradeType();
        gradeType.setTemplateId(templateId);
        List<GradeType> list = gradeTypeService.getGradeTypeOptionList(gradeType);
        mmap.put("list", JSONObject.toJSON(list));
        return prefix + "/add";
    }

    /**
     * 新增保存评分标准分类
     */
    @RequiresPermissions("system:type:add")
    @Log(title = "评分标准分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GradeType gradeType)
    {
        return toAjax(gradeTypeService.insertGradeType(gradeType));
    }

    /**
     * 修改评分标准分类
     */
    @GetMapping("/edit/{typeId}")
    public String edit(@PathVariable("typeId") Long typeId, ModelMap mmap)
    {
        List<GradeTemplate> templateList = gradeTemplateService.selectGradeTemplateList(null);
        mmap.put("templateList", JSONObject.toJSON(templateList));
        GradeType gradeType = gradeTypeService.selectGradeTypeByTypeId(typeId);
        mmap.put("gradeType", gradeType);

        GradeType condition = new GradeType();
        condition.setTemplateId(gradeType.getTemplateId());
        gradeType.setTemplateId(gradeType.getTemplateId());
        List<GradeType> list = gradeTypeService.getGradeTypeOptionList(condition);
        mmap.put("list", JSONObject.toJSON(list));
        return prefix + "/edit";
    }

    /**
     * 修改评分标准分类
     */
    @GetMapping("/getTypeByTemplateId")
    @ResponseBody
    public AjaxResult getTypeByTemplateId(Long templateId)
    {
        GradeType gradeType = new GradeType();
        gradeType.setTemplateId(templateId);
        List<GradeType> list = gradeTypeService.getGradeTypeOptionList(gradeType);
        return success(list);
    }

    /**
     * 修改保存评分标准分类
     */
    @RequiresPermissions("system:type:edit")
    @Log(title = "评分标准分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GradeType gradeType)
    {
        if("1".equals(gradeType.getScoreModel())){
            gradeType.setWeight(null);
        }

        String[] reVal=new String[2];
        reVal=gradeTypeService.updateGradeType_New(gradeType);
        if(reVal!=null && reVal[0].equals("0")) {
            return error(reVal[1].toString());
        }

        return toAjax(1);
    }

    /**
     * 删除评分标准分类
     */
    @RequiresPermissions("system:type:remove")
    @Log(title = "评分标准分类", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        int typeCount = gradeItemService.countGradeItemByTemplateIds(Convert.toStrArray(ids));
        if(typeCount>0){
            return error("该分类存在评分细则，不能删除");
        }
        return toAjax(gradeTypeService.deleteGradeTypeByTypeIds(ids));
    }


    @PostMapping( "/getAllTypes")
    @ResponseBody
    public TableDataInfo getAllTypes()
    {
        return getDataTable(gradeTypeService.selectGradeTypeList(null));
    }
}
